package com.leyou.item.controller;

import com.leyou.common.dto.BrandDTO;
import com.leyou.common.vo.PageResult;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> pageQuery(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                          @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                          @RequestParam(value = "key", required = false) String key,
                                                          @RequestParam(value = "sortBy", required = false) String sortBy,
                                                          @RequestParam(value = "desc", defaultValue = "false") Boolean desc){
        return ResponseEntity.ok(brandService.pageQuery(page, rows, key, sortBy, desc));
    }

    /**
     * 如果参数为 http://ssss?param=a,b,c,d
     * 用@RequestParam("cids")来接收的话，就可以直接转成List<String>或者String[]
     * @param brandDTO
     * @param cids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids){
        brandService.saveBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findById(@PathVariable("id") Long id){
        return ResponseEntity.ok(brandService.findById(id));
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> findByCategoryId(@RequestParam("id") Long id){
        return ResponseEntity.ok(brandService.findByCategoryId(id));
    }

    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> findBrandsByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.findByIds(ids));
    }
}
